package com.Policing.Facility_Maintenance;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.Policing.BaseActivity;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.R;
import com.Policing.Since_case.Since_case;

import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class Facility_Maintenance extends BaseActivity {
    private ImageView fh ;
    private Handler handler = new Handler();
    private String maintainId;
    private TextView deviceNumber,deviceName,part,remark;
    private LinearLayout Abnormal;
    private String url="http://"+ Ip_Set.IP+"/api/deviceMaintainQueryById";
    private Button abolish,dispose;
    @Override
    public int intiLayout() {
        return R.layout.facility_maintenance;
    }

    @Override
    public void initView() {
        deviceNumber = (TextView) findViewById(R.id.deviceNumber);
        deviceName = (TextView) findViewById(R.id.deviceName);
        part = (TextView) findViewById(R.id.part);
        remark = (TextView) findViewById(R.id.remark);
        Abnormal = (LinearLayout) findViewById(R.id.Abnormal);
        fh = (ImageView) findViewById(R.id.fh);
        abolish = (Button) findViewById(R.id.abolish);
        dispose = (Button) findViewById(R.id.dispose);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        maintainId = intent.getStringExtra("maintainId");
        deviceNumber.setText(intent.getStringExtra("deviceNumber"));
        deviceName.setText(intent.getStringExtra("deviceName"));
        part.setText(intent.getStringExtra("part"));
        fh.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
        getdata();
        Abnormal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(Facility_Maintenance.this,Abnormal_File.class);
                   intent1.putExtra("maintainId",maintainId);
                    startActivity(intent1);
            }
        });
        abolish.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                finish();
        }
    });
        dispose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(Facility_Maintenance.this,Scheme.class);
                intent1.putExtra("remark",remark.getText().toString());
                intent1.putExtra("maintainId",maintainId);
                startActivity(intent1);
                finish();
            }
        });
    }
        private void getdata(){
         new Thread(new Runnable() {
             @Override
             public void run() {
                 OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象。
                 FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
                 formBody.add("maintainId",maintainId)
                         .add("source", Since_case.set().getSource()); //传递键值对参数
                 final Request request = new Request.Builder()//创建Request 对象。
                         .url(url)
                         .post(formBody.build())//传递请求体
                         .build();
                 client.newCall(request).enqueue(new Callback() {
                     @Override
                     public void onFailure(Call call, IOException e) {

                     }

                     @Override
                     public void onResponse(Call call, Response response) throws IOException {

                         final String responseData = response.body().string();
                         Log.i("321123",responseData);
                         try {
                             final JSONObject jsonObject = new JSONObject(responseData);
                            if (jsonObject.getString("state").equals("1")){
                             final String data =   jsonObject.getString("remark");
                                handler.post(new Runnable() {
                                    @Override
                                    public void run() {
                                       remark.setText(data);
                                    }
                                });
                            }
                         } catch (JSONException e) {
                             e.printStackTrace();
                         }
                     }
                 });
             }
         }).start();
        }
}
